//
//  DDSend_friend_circle_cell.m
//  dingdang
//
//  Created by mac mini on 16/1/26.
//  Copyright © 2016年 丁当科技. All rights reserved.
//

#import "DDSend_friend_circle_cell.h"

@interface DDSend_friend_circle_cell ()
@property (weak, nonatomic) IBOutlet UIView *contentOfBody;
@property (strong,nonatomic) UIButton *addImageButton;
@end

@implementation DDSend_friend_circle_cell

- (void)awakeFromNib {
    // Initialization code
    //self.textView_body.layer.cornerRadius = 10;
    NSLog(@"进入到------->DDSend_friend_circle_cell");
    self.textView_body.layer.masksToBounds = YES;
    self.textView_body.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.textView_body.layer.borderWidth = .5;
    self.textView_body.placeholderColor = [UIColor lightGrayColor];
    self.textView_body.placeholder = @"我也写点晒晒吧！";
    //float image_start_Y = self.textView_body.frame.origin.y+self.textView_body.frame.size.height+10;
//    self.addImageButton = [[UIButton alloc] initWithFrame:CGRectMake(10,image_start_Y,(self.contentView.frame.size.width-10)/4-10,(self.contentView.frame.size.width-10)/4-10)];
    self.addImageButton = [[UIButton alloc] init];
    [self.addImageButton setBackgroundImage:[UIImage imageNamed:@"tianjia"] forState:UIControlStateNormal];
    [self.addImageButton addTarget:self action: @selector(addImageAlertView) forControlEvents: UIControlEventTouchUpInside];
    //[self.contentView addSubview:self.addImageButton];
}

-(void)clean:(UITableViewCell *)cell withImageArray:(NSArray *)imageArray{
    self.textView_body.text = @"";
    imageArray = nil;
    //self.imageView_body.image = nil;
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

//- (IBAction)btnOpenCamera:(id)sender {
//    if (self.info_delegate) {
//        if ([self.info_delegate respondsToSelector:@selector(openCamera)]) {
//            [self.info_delegate performSelector:@selector(openCamera)];
//        }
//    }
//    
//}

- (void)btnOpenCamera{
    if (self.info_delegate) {
        if ([self.info_delegate respondsToSelector:@selector(openCamera)]) {
            [self.info_delegate performSelector:@selector(openCamera)];
        }
    }
    
}


//- (IBAction)btnOpenPhotoLib:(id)sender {
//    if (self.uploadImg_num < 9) {
//        if (self.info_delegate) {
//            if ([self.info_delegate respondsToSelector:@selector(openPhotoLib)]) {
//                [self.info_delegate performSelector:@selector(openPhotoLib)];
//            }
//        }
//    }else{
//        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"晒晒发图片数量满了" message:@"最多只能上传9张" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//        [alert show];
//    }
//}

- (void)btnOpenPhotoLib{
    if (self.uploadImg_num < 9) {
        if (self.info_delegate) {
            if ([self.info_delegate respondsToSelector:@selector(openPhotoLib)]) {
                [self.info_delegate performSelector:@selector(openPhotoLib)];
            }
        }
    }else{
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"晒晒发图片数量满了" message:@"最多只能上传9张" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}



-(void)fixCell:(UITableViewCell *)cell withImageArray:(NSArray *)imageArray{
//    UIButton *addImage = [[UIButton alloc] initWithFrame:CGRectMake(10+(cell.frame.size.width-10)/3*((0+1)%3) ,220+(cell.frame.size.width-10)/3 * ((0+1)/3),(cell.frame.size.width-10)/3-10,(cell.frame.size.width-10)/3-10)];
//    [addImage setImage:[UIImage imageNamed:@"tianjia"] forState:UIControlStateNormal];
    
    self.contentOfBody.hidden = YES;
    float image_start_Y = self.textView_body.frame.origin.y+self.textView_body.frame.size.height+10;
//    if (imageArray.count==0) {
//        UIButton *addImageBtn = [[UIButton alloc] initWithFrame:CGRectMake(10,image_start_Y,(cell.frame.size.width-10)/4-10,(cell.frame.size.width-10)/4-10)];
//        //[addImageBtn setImage:[UIImage imageNamed:@"tianjia"] forState:UIControlStateNormal];
//        [addImageBtn setBackgroundImage:[UIImage imageNamed:@"tianjia"] forState:UIControlStateNormal];
//        [addImageBtn addTarget:self action: @selector(addImageAlertView) forControlEvents: UIControlEventTouchUpInside];
//        [self.contentView addSubview:addImageBtn];
//    }
    
        //清除图片
//    for (int i = 0; i < self.uploadImg_num; i++) {
//        UIImageView * imageV = (UIImageView *)[imageArray objectAtIndex:i];
//        if (imageV.superview) {
//            [imageV removeFromSuperview];//点赞进入到这里
//        }
//    }

    for (int i=0; i<imageArray.count; i++) {
        UIImageView *image_view = [[UIImageView alloc] initWithFrame:CGRectMake(10+(cell.frame.size.width-10)/4*(i%4) ,image_start_Y+(cell.frame.size.width-10)/4 * (i/4),(cell.frame.size.width-10)/4-10,(cell.frame.size.width-10)/4-10)];
        image_view.image = imageArray[i];
        [self.contentView addSubview:image_view];

    }
    
    self.addImageButton.frame = CGRectMake(10+(cell.frame.size.width-10)/4*(imageArray.count%4) ,image_start_Y+(cell.frame.size.width-10)/4 * (imageArray.count/4),(cell.frame.size.width-10)/4-10,(cell.frame.size.width-10)/4-10);
    if (imageArray.count==0) {
        [self.contentView addSubview:self.addImageButton];
    }
    
    
    
    
    
    
    self.accessoryType = UITableViewCellAccessoryNone;
    if (self.info_thisCellType == cellType_questionType_body) {
        self.contentOfBody.hidden = NO;
        //if (self.imageView_body.image) {
        //            self.contentOfBody.frame = CGRectMake(CGRectGetMinX(self.contentOfBody.frame),
        //                                                  CGRectGetMinY(self.contentOfBody.frame),
        //                                                  CGRectGetWidth(self.contentOfBody.frame),
        //                                                  CGRectGetHeight(self.contentOfBody.frame) + 70);
        //        }
        
        if (self.info_delegate) {
            self.textView_body.delegate = self.info_delegate;
        }
    }
}


-(void)addImageAlertView{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil
                                                        message:nil
                                                       delegate:self
                                              cancelButtonTitle:@"取消"
                                              otherButtonTitles:@"从手机相册选择",@"拍照", nil];
    
    
    [alertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    switch (buttonIndex) {
        case 0:
            //NSLog(@"Cancel button clicked");
            break;
        case 1:
            //NSLog(@"相册选择图片");
            [self btnOpenPhotoLib];
            break;
        case 2:
            //NSLog(@"拍摄");
            [self btnOpenCamera];
            break;
            
        default:
            break;
    }
}



@end
